package com.atguigu.gmall.publisherlj.controller;

import com.atguigu.gmall.publisherlj.bean.TradeStats;

import com.atguigu.gmall.publisherlj.service.TradeStatsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author 晶晶
 * @Date 2023 03 29 14 31
 **/
@RestController
@RequestMapping("/sugar")
public class Controller {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/province")
    public String getTradeOrderCnt(@RequestParam(value = "date", defaultValue = "0") int date){

        if (date==0){
            date = getToday();
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");

        //查询数据
        List<Map> tradeProvinceOrderCt = tradeStatsService.getTradeProvinceOrderCt(date);
        //System.out.println(tradeProvinceOrderCt);
        for (int i = 0; i < tradeProvinceOrderCt.size(); i++) {

            Map map = tradeProvinceOrderCt.get(i);

            Object province_name = map.get("province_name");
            Object order_count = map.get("order_count");
            Object order_user_id_count = map.get("order_user_id_count");
            Object order_amount = map.get("order_amount");

            stringBuilder.append("{")
                    .append("\"province_name\":\"")
                    .append(province_name)
                    .append("\",")
                    .append("\"order_count\":\"")
                    .append(order_count)
                    .append("\",")
                    .append("\"order_user_id_count\":\"")
                    .append(order_user_id_count)
                    .append("\",")
                    .append("\"order_amount\":\"")
                    .append(order_amount)
                    .append("\"}");

            if (i==tradeProvinceOrderCt.size()-1){
                stringBuilder.append("]");
            }else {
                stringBuilder.append(",");
            }
        }


        return "{\n" +
                "  \"status\": 0," +
                "  \"msg\": \"\"," +
                "  \"data\": {\n" +
                "    \"total\": 34, " +
                "    \"columns\": [" +
                "      {" +
                "        \"name\": \"province_name\"," +
                "        \"id\": \"province_name\"" +
                "      }," +
                "      {" +
                "        \"name\": \"order_count\"," +
                "        \"id\": \"order_count\"" +
                "      }," +
                "      {" +
                "        \"name\": \"order_user_id_count\"," +
                "        \"id\": \"order_user_id_count\"" +
                "      }," +
                "      {" +
                "        \"name\": \"order_amount\"," +
                "        \"id\": \"order_amount\"" +
                "      }" +
                "    ]," +
                "    \"rows\": "+stringBuilder +
                "  }" +
                "}";

    }



    @RequestMapping("/TradeOrderAmount")
    public String getTradeStatistics(@RequestParam(value = "date",defaultValue = "0") int date){

        if (date==0){
            date=getToday();
        }

        BigDecimal orderAmount = tradeStatsService.getOrderAmount(date);
        if (orderAmount==null){
            return "0";
        }

        return "{ " +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\"," +
                "  \"data\": {" +
                "    \"categories\": [" +
                "      \"order_amount\"" +
                " " +
                "    ],\n" +
                "    \"series\": [" +
                "      {" +
                "        \"name\": \"order_amount\"," +
                "        \"data\": ["+ orderAmount +" ]" +
                "      }" +
                "    ]" +
                "  }" +
                "}";

    }

    @RequestMapping("/TradeStatics")
    public String getStats(@RequestParam(value = "date",defaultValue = "0") int date) {

        if (date == 0) {
            date = getToday();
        }

        List<TradeStats> tradeStatsList = tradeStatsService.getTradeStats(date);

        if (tradeStatsList == null) {
            return "0";
        }

        TradeStats tradeStats = tradeStatsList.get(0);
        ArrayList<Long> integers = new ArrayList<>();
        String type = tradeStats.getType();
        if ("下单人次".equals(type)){
            integers.add(tradeStats.getOrderCt());
            integers.add(tradeStatsList.get(1).getOrderCt());
        }else {
            integers.add(tradeStatsList.get(1).getOrderCt());
            integers.add(tradeStats.getOrderCt());
        }


        return "{ " +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\"," +
                "  \"data\": {" +
                "    \"categories\":[  " +
                "  \"下单人次\","+
                "  \"下单人数\""+
                 "],\n" +
                "    \"series\": [" +
                "      {" +
                "        \"name\": \"\"," +
                "        \"data\":  " + integers +
                "      }" +
                "    ]" +
                "  }" +
                "}";

    }


    private int getToday(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(System.currentTimeMillis()));
    }
}
